package com.nidu.demo.oauth2.executor.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.oauth2.convertor.OAuth2ClientCriteriaConvertor;
import com.nidu.demo.oauth2.dto.OAuth2ClientListQry;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.oauth2.convertor.OAuth2ClientConvertor;
import com.nidu.demo.oauth2.dto.OAuth2ClientCO;
import com.nidu.demo.oauth2.gateway.OAuth2ClientGateway;
import com.nidu.demo.oauth2.model.OAuth2Client;
import com.nidu.demo.oauth2.model.OAuth2ClientCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class OAuth2ClientListQryExe {

    private final OAuth2ClientGateway gateway;

    private final OAuth2ClientConvertor convertor;

    private final OAuth2ClientCriteriaConvertor criteriaConvertor;

    public MultiResponse<OAuth2ClientCO> execute(OAuth2ClientListQry qry) {
        OAuth2ClientCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<OAuth2Client> auth2Clients = gateway.listByCondition(criteria);
        if(CollUtil.isEmpty(auth2Clients)){
            throw new BizException(ErrorCodeConstants.OAUTH2_CLIENT_NOT_FOUND.getCode(), ErrorCodeConstants.OAUTH2_CLIENT_NOT_FOUND.getMessage());
        }
        return MultiResponse.of(convertor.toClientObjects(auth2Clients));
    }

}
